package com.sc.web.gwt.client.presenter;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.sc.web.gwt.client.ClientFactory;
import com.sc.web.gwt.client.GetTokenKeyService;
import com.sc.web.gwt.client.GetTokenKeyServiceAsync;
import com.sc.web.gwt.client.place.AdministratorWidgetPlace;
import com.sc.web.gwt.client.view.AdministratorWidget.AdministratorWidgetView;

public class AdministratorActivity extends AbstractActivity implements AdministratorWidgetView.Presenter {
    // Used to obtain views, eventBus, placeController
    // Alternatively, could be injected via GIN
    private ClientFactory clientFactory;
    private GetTokenKeyServiceAsync getTokenKeyService = (GetTokenKeyServiceAsync) GWT.create(GetTokenKeyService.class);
    private AdministratorWidgetView administratorWidgetView;

    public AdministratorActivity(AdministratorWidgetPlace place, ClientFactory clientFactory)  {
        this.clientFactory = clientFactory;
    }

    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);        
    }

    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        administratorWidgetView = clientFactory.getAdministratorWidgetView();
        administratorWidgetView.setPresenter(this);
        panel.setWidget(administratorWidgetView.asWidget());
    }

    public void generateCode() {
        getTokenKeyService.getTokenKey(new AsyncCallback<String>()  {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }

            public void onSuccess(String result) {
                administratorWidgetView.setTokenLabel(result);
            }
        });
    }
}